/*
 * Internationalization.java
 *
 * Created on 9 November 2006, 19:06
 *
 */

package org.foment.joliage3.core.internationalization;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.ResourceBundle;

/**
 *
 * @author ivan
 */
public class Internationalization {
  
  /** Creates a new instance of Internationalization */
  public Internationalization() {
  }
  
  public static String i18n(String text) {
    if (_translations.containsKey(text))
      text = _translations.get(text);
    return text;
  }
  
  private static HashMap< String, String > _translations;
  private static String _language;
  
  public static void initializeTranslations(String translationIndex, String language) {
    LinkedList < String > bundles = new LinkedList();
    _translations = new HashMap();
   
    bundles.add(translationIndex);
    _language = language;
    
    while (bundles.size() != 0) {
      ResourceBundle bundle = ResourceBundle.getBundle(bundles.getFirst());
      bundles.removeFirst();
      int count = Integer.parseInt(bundle.getString("translations.count"));
      
      for (int index = 0; index < count; index++) {
        try {
          try {
            String currentName = bundle.getString("translations." + index + ".name");
            String translationFile = bundle.getString("translations." + index + ".translationFile");
            loadTranslation(translationFile);
            notifyTranslationLoaded(currentName);
          } catch (Exception ex) { }

          try {
            String indexFile = bundle.getString("translations." + index + ".indexFile");
            bundles.addLast(indexFile);
          } catch (Exception ex) { }
          
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
      
    }
  }

  private static void loadTranslation(String translationFile) {
    InputStream isKeys = Internationalization.class.getResourceAsStream(translationFile + "_keys.i18n");
    InputStream isValues = Internationalization.class.getResourceAsStream(translationFile + "_" + _language + ".i18n");

    BufferedReader inKeys = new BufferedReader(new InputStreamReader(isKeys, java.nio.charset.Charset.forName("UTF-8")));
    BufferedReader inValues = new BufferedReader(new InputStreamReader(isValues, java.nio.charset.Charset.forName("UTF-8")));
    
    String key, value;

    try {
      while (((key = inKeys.readLine()) != null) && ((value = inValues.readLine()) != null)) {
        if (!_translations.containsKey(key))
          _translations.put(key, value);
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    
  }
  
  private static LinkedList <InitializationListener> _listeners = new LinkedList();
  
  private static void notifyTranslationLoaded(String name) {
    for (InitializationListener listener: _listeners)
      listener.translationLoaded(name);
  }

  private static void notifyTranslationsInitialized() {
    for (InitializationListener listener: _listeners)
      listener.translationsInitialized();
  }

  
  public static void addListener(InitializationListener listener) {
    if (!_listeners.contains(listener))
      _listeners.add(listener);
  }
  
  public static void removeListener(InitializationListener listener) {
    if (_listeners.contains(listener))
      _listeners.remove(listener);
  }
  
  public interface InitializationListener {
    public void translationLoaded(String name);
    public void translationsInitialized();
  }
  
}
